//
//  MineViewController.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "MineViewController.h"
#import "HeaderView.h"
#import "MineTableViewCell.h"
#import "KeleNavigationController.h"
#import "LoginViewController.h"
#import "CellTintViewModel.h"
#import "CellTintModel.h"
#import "MyCollectionViewController.h"
#import "AttentionViewController.h"
#import "UploadViewController.h"
#import "ScoreViewController.h"
#import "WatchViewController.h"
#import "MyPraseViewController.h"
#import "SettingViewController.h"
#import "PersonalPageViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HeaderView *headerView;
@property (nonatomic, strong) UIImageView *profileImageView; // 头像
@property (nonatomic, strong) NSMutableArray *dataArray; // 数据源
@property (nonatomic, strong) NSMutableArray *sectionOneData;
@property (nonatomic, strong) NSMutableArray *sectionTwoData;
@property (nonatomic, strong) NSMutableArray *sectionThreeData;
@end


static  NSString *mineCell = @"mineCell";
@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    // 调取用户信息接口  要更新数据
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureMineVC];
    [self loadSubviewsOnMine];
    [self loadClicks];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:LOGIN object:nil] subscribeNext:^(id x) {
        [self gotoLogin];
    }];
}

// 初始配置
- (void)configureMineVC{
    self.view.backgroundColor = KLColor_C1;
    self.dataArray = [CellTintViewModel returnTintData];
    for (int i = 0; i < self.dataArray.count; i++) {
        CellTintModel *model = self.dataArray[i];
        if (i == 0) {
            [self.sectionOneData addObject:model];
        }else if (i > 0 &&i< 4){
            [self.sectionTwoData addObject:model];
        }else{
            [self.sectionThreeData addObject:model];
        }
    }
    
}

// 加载视图
- (void)loadSubviewsOnMine{
    UIView *topBgView = [[UIView alloc]init];
    topBgView.backgroundColor = KLColor_A1;
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(100*HEIGHT);
    }];
    
    
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingBtn setBackgroundImage:[UIImage imageNamed:@"info_shezhi"] forState:UIControlStateNormal];
    [topBgView addSubview:settingBtn];
    [[settingBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        SettingViewController *settingVc = [[SettingViewController alloc]init];
        [self.navigationController pushViewController:settingVc animated:YES];
    }];
    [settingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(22*WIDTH);
        make.height.mas_equalTo(23*HEIGHT);
        make.right.equalTo(topBgView).offset(-30*WIDTH);
        make.bottom.equalTo(topBgView).offset(-27*HEIGHT);
    }];
    
    [self loadTableView];
    
    
    UIView *profileBgView = [[UIView alloc]init];
    profileBgView.backgroundColor = KLColor(255, 255, 255, 0.28);
    profileBgView.layer.cornerRadius = 40*WIDTH;
    [self.view addSubview:profileBgView];
    [profileBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(60*HEIGHT);
        make.width.height.mas_equalTo(80*WIDTH);
    }];
    
    UIImageView  *profileImageView = [[UIImageView alloc]init];
    profileImageView.image = [UIImage imageNamed:@"info_touxiang"];
    self.profileImageView = profileImageView;
    profileImageView.contentMode = UIViewContentModeScaleAspectFit;
    profileImageView.layer.cornerRadius = 34*WIDTH;
    [profileBgView addSubview:profileImageView];
    profileImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapProfile = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapProfile)];
    [profileImageView addGestureRecognizer:tapProfile];
    [profileImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(profileBgView);
        make.width.height.mas_equalTo(68*WIDTH);
    }];
}


// loadTableView
- (void)loadTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100*HEIGHT,KLScreenW,KLScreenH-100*HEIGHT-50) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.sectionFooterHeight = 10*HEIGHT;
    tableView.sectionHeaderHeight = 0;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.headerView = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 183*HEIGHT)];
    tableView.tableHeaderView = self.headerView;
    self.tableView = tableView;
}

#pragma mark  ---- tableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.sectionOneData.count;
    }else if (section == 1){
        return self.sectionTwoData.count;
    }
    // 要分登录状态  4和   非登录状态5
    return self.sectionThreeData.count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCell];
    if (!cell) {
        cell = [[MineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:mineCell];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CellTintModel *model;
    if (indexPath.section == 0) {
        model = self.sectionOneData[indexPath.row];
    }else if (indexPath.section == 1){
        model = self.sectionTwoData[indexPath.row];
        if (indexPath.row < self.sectionTwoData.count-1) {
            cell.isHiddenLine = NO;
        }
    }else if (indexPath.section == 2){
        model = self.sectionThreeData[indexPath.row];
        if (indexPath.row < self.sectionThreeData.count-1) {
            cell.isHiddenLine = NO;
        }
    }
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*HEIGHT;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            AttentionViewController *attenVc = [[AttentionViewController alloc]init];
            [self.navigationController pushViewController:attenVc animated:YES];
        }
        
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            UploadViewController *uploadVc = [[UploadViewController alloc]init];
            [self.navigationController pushViewController:uploadVc animated:YES];
        }
        if (indexPath.row == 1) {
            [SVProgressHUD showAlert:@"待开放"];
        }
        
        if (indexPath.row == 2) {
            ScoreViewController *scoreVc = [[ScoreViewController alloc]init];
            [self.navigationController pushViewController:scoreVc animated:YES];
        }
        
        
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            WatchViewController *watchVc = [[WatchViewController alloc]init];
            [self.navigationController pushViewController:watchVc animated:YES];
        }
        if (indexPath.row == 1) {
            MyCollectionViewController *collectionVc = [[MyCollectionViewController alloc]init];
            [self.navigationController pushViewController:collectionVc animated:YES];
        }
        
        if (indexPath.row == 3) {
            MyPraseViewController *myPraseVc = [[MyPraseViewController alloc]init];
            [self.navigationController pushViewController:myPraseVc animated:YES];
        }
        
        if (indexPath.row == 4) {
            [SVProgressHUD showAlert:@"待开放"];
        }
    }

    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 10*HEIGHT)];
    bgView.backgroundColor = KLColor_C1;
    return bgView;
}

#pragma mark ---- 加载点击事件
- (void)loadClicks{
    [[self.headerView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
//        LoginViewController *loginVc = [[LoginViewController alloc]init];
//        KeleNavigationController *nav = [[KeleNavigationController alloc]initWithRootViewController:loginVc];
//        [self.navigationController pushViewController:loginVc animated:YES];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:LOGIN object:nil];
    }];
}



#pragma mark --- 登录
- (void)gotoLogin{
    LoginViewController *loginVc = [[LoginViewController alloc]init];
    KeleNavigationController *nav = [[KeleNavigationController alloc]initWithRootViewController:loginVc];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}


- (void)tapProfile
{
    PersonalPageViewController *personalVc = [[PersonalPageViewController alloc]init];
    [self.navigationController pushViewController:personalVc animated:YES];
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)sectionOneData{
    if (!_sectionOneData) {
        _sectionOneData = [NSMutableArray array];
    }
    return _sectionOneData;
}

- (NSMutableArray *)sectionTwoData{
    if (!_sectionTwoData) {
        _sectionTwoData = [NSMutableArray array];
        
    }
    return _sectionTwoData;
}
- (NSMutableArray *)sectionThreeData{
    if (!_sectionThreeData) {
        _sectionThreeData = [NSMutableArray array];
    }
    return _sectionThreeData;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
